package com.macro.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.LoanPlan;
import com.macro.mall.portal.domain.User;
import com.macro.mall.portal.domain.vo.PageVo;
import com.macro.mall.portal.domain.vo.WxMsgVo;
import com.macro.mall.portal.service.ILoanPlanService;
import com.macro.mall.portal.service.IUserService;
import com.macro.mall.portal.util.ConfigConstant;
import com.macro.mall.portal.util.InfoFilterUtil;
import com.macro.mall.portal.util.WxMsgPush;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 前端控制器
 *
 * @author
 * @since 2021-09-14
 */
@RestController
@RequestMapping("/loanPlan")
@Api(tags = "专属方案", description = "专属方案")
public class LoanPlanController {

    @Autowired
    private ILoanPlanService loanPlanService;

    @Autowired
    private WxMsgPush wxMsgPush;

    @Autowired
    private IUserService vxUserService;


    @ApiOperation("专属方案提交")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult addOne(@RequestBody LoanPlan loanPlan) {
        try {
            BigDecimal loanMoney = new BigDecimal(0);
            switch (loanPlan.getLoanType()) {
                case 1:
                    BigDecimal house = new BigDecimal(0.7).multiply(new BigDecimal(loanPlan.getLoanMoney()));
                    loanMoney = new BigDecimal(10000).multiply(house);
                    break;
                case 2:
                    BigDecimal multiply = new BigDecimal(10000).multiply(new BigDecimal(15.8).multiply(new BigDecimal(loanPlan.getLoanMoney())));
                    //开票
                    BigDecimal invoice = new BigDecimal(10000).multiply(new BigDecimal(0.208).multiply(loanPlan.getInvoice()));

                    loanMoney = invoice;
                    if (multiply.compareTo(invoice) == 1) {
                        loanMoney = multiply;
                        System.out.println("a大于b");
                    }
                    break;
                case 3://工薪

                    BigDecimal fund = new BigDecimal(40.8).multiply(new BigDecimal(loanPlan.getLoanMoney()));
                    //工资
                    BigDecimal wages = new BigDecimal(30.8).multiply(loanPlan.getWages());
                    loanMoney = wages;
                    if (fund.compareTo(wages) == 1) {
                        loanMoney = fund;
                        System.out.println("a大于b");
                    }
                    break;
                case 4:
                    BigDecimal call = new BigDecimal(15).multiply(new BigDecimal(loanPlan.getLoanMoney()));
                    loanMoney = new BigDecimal(10000).multiply(call);
                    break;
                default:
            }

            loanPlan.setBudget(loanMoney);
            loanPlanService.save(loanPlan);

            String url = ConfigConstant.TD_URL + "?id=" + loanPlan.getId();
            LocalDateTime time = LocalDateTime.now();

            DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            String now = dtf2.format(time);
            if (loanPlan.getJudge() == 1) {
                loanPlan.setOpenId("oK1zE5mwv4zVhoRPIhcSn5H3wksY");
            }
            //todo 文案
            WxMsgVo msgVo = WxMsgVo.builder()
                    .first("您好！客户专属方案已经提交")
                    .keyword1(loanPlan.getName() + "｜" + loanPlan.getPhone())
                    .keyword2(now)
                    .openId(loanPlan.getOpenId())
                    .url(url)
                    .remark("客户不跟踪万事一场空！点击查看详情进行快速跟进")
                    .build();
            wxMsgPush.SendWxMsg(msgVo);

            //上级领取优惠价
            QueryWrapper<User> user = new QueryWrapper<>();
            user.eq("open_id", loanPlan.getOpenId());
            User one = vxUserService.getOne(user);
            if (one.getSuperId() != 0) {
                User sub = vxUserService.getById(one.getSuperId());
                String url2 = ConfigConstant.TD_URL3 + "?id=" + loanPlan.getId();
                WxMsgVo msg = WxMsgVo.builder()
                        .first("您好！下级合伙人推荐客户专属方案已提交")
                        .keyword1(InfoFilterUtil.maskUserName(loanPlan.getName()) + "｜" + InfoFilterUtil.mobileEncrypt(loanPlan.getPhone()))
                        .keyword2(now)
                        .openId(sub.getOpenId())
                        .url(url2)
                        .remark("客户不跟踪万事一场空！点击查看详情进行快速跟进")
                        .build();
                wxMsgPush.SendWxMsg(msg);
            }


            return CommonResult.success(loanMoney, "提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("分页查询专属方案记录")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public CommonResult pageList(@RequestBody PageVo page) {
        PageHelper.startPage(page.getPage(), page.getSize());
        try {
            QueryWrapper<LoanPlan> wrapper = new QueryWrapper<>();
            wrapper.eq("open_id", page.getOpenId());
            List<LoanPlan> loanPlanList = loanPlanService.list(wrapper);
            PageInfo<LoanPlan> pageInfo = new PageInfo<>(loanPlanList, page.getSize());
//todo 收益
//            loanPlanList.stream().map(l)
//
//            BigDecimal gameDraw = loanPlanList.stream()
//                    .filter(
//                            (LoanPlan l) -> l.getLoanStatus() == 4
//                    )
//                    .map(
//                            LoanPlan::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add
//                    );
//
//            LoanPlanDto dto = LoanPlanDto.builder()
//                    .pageInfo(pageInfo)
//                    .finalIncome(ga)
//                    .build();
            return CommonResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("查询专属方案进展")
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public CommonResult getInfo(@RequestBody LoanPlan plan) {

        try {
            QueryWrapper<LoanPlan> wrapper = new QueryWrapper<>();
            wrapper.eq("id", plan.getId());
            LoanPlan loanPlan = loanPlanService.getOne(wrapper);
            return CommonResult.success(loanPlan);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("用户查询个人专属方案")
    @RequestMapping(value = "/getRecord", method = RequestMethod.POST)
    public CommonResult getRecord(@RequestBody PageVo page) {
        PageHelper.startPage(page.getPage(), page.getSize());
        try {
            QueryWrapper<LoanPlan> wrapper = new QueryWrapper<>();
            wrapper.eq("phone", page.getPhone());
            List<LoanPlan> loanPlanList = loanPlanService.list(wrapper);
            PageInfo<LoanPlan> pageInfo = new PageInfo<>(loanPlanList, page.getSize());
            return CommonResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }


}
